<?php
namespace app\shop\model;

use app\BaseModel;
use think\facade\Db;


class AdminShopOrderGoods extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_shop_order_item';

    /**
     *
     * 获取自订单详情
     */
    public function orderGoodsInfo($dis){
       return self::where($dis)->select()->toArray();
    }

    /**
     * @param $dis
     * 获取转换了的子订单
     */
    public function changeInfo($dis){
        $order = $this->orderGoodsInfo($dis);
        $data['number']    = 0;
        $data['good_info'] ='';
        if(!empty($order)){
            foreach ($order as $item){
                $data['number']    += $item['number'];
                $data['good_info'] .= '商品名: ' . $item['name'] . ', 规格: ' . $item['content'] . ', 数量: ' . $item['number'] . '; ';
            }
        }
        return $data;
    }

    public function getGoosNember($where){
        $data = $this->alias('a')
            ->join('longbing_card_shop_order b', 'a.order_id = b.id and b.status=1 and pay_status=1 and order_status=3')
            ->join('longbing_card_company c', 'b.company_id = c.id and c.status=1')
            ->field('a.name,sum(a.number) as number,a.id,a.goods_id,b.company_id')
            ->where($where)
            ->group('a.goods_id')
            ->order('number','desc')
            ->limit(5)
            ->select()
            ->toArray();
        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2021-01-04 15:50
     * @功能说明:获取实际的商品销量
     */
    public function getTrueSaleNum($goods_id,$uniacid){

        $dis[] = ['a.uniacid','=',$uniacid];

        $dis[] = ['a.uniacid','=',$uniacid];

        $dis[] = ['b.goods_id','=',$goods_id];

        $dis[] = ['a.pay_status','in',[0,1]];

        $dis[] = ['a.order_status','<>',1];

        $dis[] = ['a.refund_status','<>',2];

        $num = Db::name('longbing_card_shop_order')->alias('a')
               ->join('longbing_card_shop_order_item b','a.id = b.order_id')
               ->where($dis)
               ->group('a.id')
               ->sum('b.number');

        return $num;
    }


}